// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import './scss/reset.css';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import 'swiper/dist/css/swiper.css';
import VueAwesomeSwiper from 'vue-awesome-swiper'
Vue.use(VueAwesomeSwiper)


Vue.use(ElementUI);
import imgUrl from './api/img';
Vue.config.productionTip = false
Vue.prototype.imgUrl = imgUrl; // 挂载到Vue实例上面
let token = localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')).token : null;
router.beforeEach(async(to, from, next) => {
  if (to.meta.isLogin) { // 判断是否需要登录才能渲染页面
    if (store.getters.token) {
    // 如果有token，表明已经登录，如果url输入login，则到首页，不跳转到登录页面
      if (to.path === '/login') {
        next({
          path: '/'
        });
      } else {
      // 否则放行
        next();
      }
    } else {
      if (to.path === '/login') {
      // 如果没有登录，是替换到登录页面，则放行
        next();
      } else {
      // 否则直接替换到登录页面
        next({
          path: '/login'
          //   replace: true
        });
      }
    }
  } else {     
      next();
 
  }

});

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
